<?php

namespace app\common\business;

use app\common\model\mysql\SysDictType as SysDictTypeModel;
use app\common\model\mysql\SysDictData as SysDictDataModel;

class SysDictType
{
    public $dictTypeObj = null;
    public $dictDataObj = null;
    public function __construct()
    {
        $this->dictTypeObj = new SysDictTypeModel();
        $this->dictDataObj = new SysDictDataModel();
    }

    /**
     * 查询字典类型列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalDictType($where,$field,$limit,$page){
        $dictTypeData = $this->dictTypeObj->getNormalDictType($where,$field,$limit,$page);
        if(!$dictTypeData){
            return [];
        }
        $dictTypeData = $dictTypeData->toArray();
        return $dictTypeData;
    }

    //添加字典类型
    public function addDictType($data){
        //根据$dict_name去数据库查询是否存在这条记录
        $postNameResult = $this->getDictByName($data['dict_name']);
        if($postNameResult){
            throw new \think\Exception("该字典名称已经存在请重新设置");
        }
        //根据$dict_type去数据库查询是否存在这条记录
        $postCodeResult = $this->getDictByType($data['dict_type']);
        if($postCodeResult){
            throw new \think\Exception("该字典类型已经存在请重新设置");
        }
        try {
            $this->dictTypeObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->dictTypeObj->id;
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getDictTypeById($id){
        $user = $this->dictTypeObj->getDictByTypeId($id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    public function getDictByName($dict_name,$dict_id = ''){
        $user = $this->dictTypeObj->getDictByPostName($dict_name,$dict_id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    public function getDictByType($dict_type,$dict_id = ''){
        $user = $this->dictTypeObj->getDictByDictType($dict_type,$dict_id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    /**
     * 查询字典类型总条数
     * @param $where
     * @return int
     */
    public function getNormalDictTypeCount($where){
        $postCount = $this->dictTypeObj->getNormalDictCount($where);
        if(!$postCount){
            return 0;
        }
        return $postCount;
    }

    /**
     * 根据主键id更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        //根据$dict_name去数据库查询是否存在这条记录
        $postNameResult = $this->getDictByName($data['dict_name'],$id);
        if($postNameResult){
            throw new \think\Exception("该字典名称已经存在请重新设置");
        }
        //根据$dict_type去数据库查询是否存在这条记录
        $postCodeResult = $this->getDictByType($data['dict_type'],$id);
        if($postCodeResult){
            throw new \think\Exception("该字典类型已经存在请重新设置");
        }
        $where = [
            "dict_id"=>$id,
        ];
        return $this->dictTypeObj->where($where)->find()->save($data);
    }

    //根据$ids数组，删除数据库记录delete
    public function DeldictTypeIds($ids){
        $ids = explode(',', $ids);
        $dictTypes = $this->dictTypeObj->getDictTypeIds($ids);
        //根据$dictTypes数组去数据库查询是否存在这条记录
        $postResult = $this->dictDataObj->getDictDataByDataModel($dictTypes);
        if($postResult){
            $postData = $this->getDictByType($postResult->dict_type);
            throw new \think\Exception($postData['dict_name']."已有下级,请先删除下级");
        }
        $result = $this->dictTypeObj->DelDictTypeIds($ids);
        return $result;
    }
}
